package com.itheima.ydd.controller;

import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddLink;
import com.itheima.ydd.service.YddLinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/ydd_link")
public class YddLinkController {
    @Autowired
    private YddLinkService yddLinkService;

    /**
     * 修改 -- 回显数据
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id){
        log.info("修改友情链接id: {}",id);
        YddLink yddLink = yddLinkService.getById(id);
        return ResultData.ok(null,yddLink);
    }

    /**
     * 修改
     * @param yddLink
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddLink yddLink){
        log.info("修改友情链接: {}",yddLink);
        yddLinkService.updateById(yddLink);
        return ResultData.ok("修改成功",null);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(Integer id){
        log.info("删除友情链接id: {}",id);
        yddLinkService.removeById(id);
        return ResultData.ok("删除成功",null);
    }

    /**
     * 添加
     * @param yddLink
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddLink yddLink){
        log.info("添加友情链接: {}",yddLink);
        yddLinkService.save(yddLink);
        return ResultData.ok("添加成功",null);
    }

    /**
     * 分页条件查询
     */
    @GetMapping("/list")
    public ResultData getList(String type, Long page, Long limit){
        log.info("类型:{}, 页码:{}, 每页数量:{}",type,page,limit);
        PageResult pageResult = yddLinkService.getList(type,page,limit);
        return ResultData.ok("",pageResult);
    }
}
